#include <linux/init.h>
#include <linux/module.h>
#include <linux/netfilter_ipv4.h>
#include <linux/list.h>
#include <net/ip.h>
#include "../include/pub.h"
#include "uf_rule.h"
#include "uf_rule_adv.h"

#define UF_DNS_PORT    (53)

unsigned int UF_match_rule_https(UF4_MatchInfo_S *pfilter, void *packet)
{
    unsigned int ret = NF_ACCEPT;
    UF_kruleinfo_S *prule = NULL;
    struct list_head *pos, *temp;
    struct ts_state state;
    int match_ret = 0;
    struct sk_buff *skb = (struct sk_buff *)packet;

    read_lock_irq(&urlrule_lock);
    list_for_each_safe(pos, temp, &UF_rulelist) 
    {
		prule = list_entry(pos, UF_kruleinfo_S, list);

        /* url */
        if ((NULL != prule->match_url) && (!IS_ERR(prule->match_url)) &&
            (UINT_MAX != skb_find_text(skb, 0, skb->len, prule->match_url, &state)))
        {
            match_ret = 1;
            ret = prule->rule.action;
            if (match_debugging)
            {
                printk("<0>""match: key=%s, action=%s\n", 
                       prule->rule.url, prule->rule.action ? "pass" : "drop");
                match_count++;
                if (match_count > 10)
                {
                    match_debugging = 0;
                    match_count = 0;
                }
            }
            break;
        }
	}
    if (!match_ret)
    {
        ret = default_act;
        if (match_debugging)
        {
            printk("<0>""match: default, action=%s\n", default_act ? "pass" : "drop");
            default_count++;
            if (default_count > 10)
            {
                default_debugging = 0;
                default_count = 0;
            }
        }
    }
    read_unlock_irq(&urlrule_lock);

    return ret;
}

unsigned int UF_match_https(UF4_MatchInfo_S *pfilter, void *packet)
{
    unsigned int ret = NF_ACCEPT;

    ret = UF_match_rule_https(pfilter, packet);

    return ret;
}
